package twentyThreePattern.create5.simple_factory.section6;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * 工厂方法-延迟加载类
 * 
 * @author kongmou
 * @date 2018年3月1日上午10:40:07
 * @since jdk 1.8
 * 
 * @version 1.0
 */
public class ProductFactory {

	private static Map<String, Product> preProducts = new HashMap<>();

	public static synchronized Product createProduct(ProductKind kind) {
		Product product = null;
		if (preProducts.containsKey(kind.getName())) {
			return preProducts.get(kind.getName());
		} else {
			if (ProductKind.PRODEUCT1.getName().equals(kind.getName())) {
				product = new ConcretProduct1();
			} else {
				product = new ConcretProduct2();
			}
			// 同时把对象放到缓存容器中
			preProducts.put(kind.getName(), product);
		}

		return product;

	}
}
